﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Diagnostics;

namespace EntityEditorLib
{
    /// <summary>
    /// Settings required for creating a new list.
    /// </summary>
    public class ListSettings
    {
        private SPSite site;
        private SPWeb web;
        public ListSettings(SPSite site, SPWeb web)
        {
            this.site = site;
            this.web = web;
        }

        public string title;
        public string description;

        public SPListTemplate template
        {
            get
            {
                return web.ListTemplates[templateStr];
            }
        }
        public string templateStr;
        public List<SPContentType> ctCollection
        {
            get
            {
                string[] ct = contentTypesStr.Split(';');
                List<SPContentType> ctCollection = new List<SPContentType>();
                foreach (string c in ct)
                {
                    //get the ct
                    SPContentType curCT = web.ContentTypes[c.Trim()];
                    //add to col
                    ctCollection.Add(curCT);

                }
                return ctCollection;
            }
        }
        public string contentTypesStr;
        public bool isOnQuickLaunch;
        public bool isAllowContentTypes;
        public bool isEnableFolderCreation;
        public bool isEnableVersioning;

    }

    /// <summary>
    /// Higher level class to manage lists.
    /// </summary>
    public class ListManagementLib
    {
        private string SITE_URL;
        private SPSite site;
        private SPWeb web;

        public ListManagementLib(string SITE_URL)
        {
            this.SITE_URL = SITE_URL;
            this.site = new SPSite(this.SITE_URL);//Dispose
            this.web = site.OpenWeb();//Dispose
        }

        public void Dispose()
        {
            
            this.web.Dispose();
            this.site.Dispose();
        }

        public ListSettings GetListSettings(string listName)
        {
            ListSettings settings = new ListSettings(site, web);
            //TODO: Populate list settings here!
            return settings;

        }

        /// <summary>
        /// Creates a new list based on the settings. If the list exists (by name) then create is ignored. 
        /// </summary>
        /// <param name="settings"></param>
        /// <returns></returns>
        public   SPList CreateList(ListSettings settings)
        {

            //string title, string description, SPListTemplate template, List<SPContentType> ctCollection, bool isOnQuickLaunch)
            //GET
            SPList curList;
            try
            {
                curList = web.Lists[settings.title];
            }
            catch (ArgumentException ex)
            {
                //create
                web.Lists.Add(settings.title, settings.description, settings.template);
                web.Update();
                curList = web.Lists[settings.title];

                curList.Update();
                Debug.WriteLine("Created CT : " + settings.title);
            }

            return default(SPList);//TODO:FIX
        }
    }
}
